<?php

namespace App\Admin\Controllers;

use App\UsersOrders;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Actions\Post\RefuseAuditFail;
use App\Admin\Actions\Post\RefuseAuditOk;

class OrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '订单列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $arr = [
            0=>'待传订单',
            10=>'初审驳回',
            20=>'已提交待初审',
            25=>'待传品鉴',
            30=>'复审驳回',
            32=>'等待平台复审',
            35=>'复审中',
            40=>'复审通过',
            50=>'已结算',
            60=>'已取消',
        ];
        $grid = new Grid(new UsersOrders());

        $grid->model()->orderBy('id', 'desc');

        $grid->filter(function($filter) use ($arr) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->like('order_no', '订单编号');
            $filter->where(function ($query) {
                $query->whereHas('user', function ($query) {
                    $query->where('name', '=', "{$this->input}")->orWhere('phone', '=', "{$this->input}");
                });

            }, '买家名称');

            $filter->equal('store_uid', '商户ID');

            // 在这里添加字段过滤器
            $filter->like('store_title', '店铺名称');
            // 设置datetime类型
            $filter->between('created_at', '下单时间')->datetime();
            $filter->in('status', '订单状态')->multipleSelect($arr);

        });

        $grid->column('id', __('Id'));
        $grid->column('order_no', '订单编号')->width(200)->display(function($value){
            return $value.'<br/>'.$this->user_order_no;
        });
        $grid->column('store_title', '任务信息')->width(150)->display(function($title){
            return $title.'<br>'.($this->good_comment_type?config('app.good_comment_type')[$this->good_comment_type]:'无需好评');
        });
        $grid->column('max_return', '满返')->display(function() {
            return "满".$this->max_money."返".$this->return_money;
        });
        $grid->column('user.name', '买家')->display(function($value){
            return $value.'<br/>'.$this->user->phone;
        });
        $grid->column('created_at', '下单时间');
        $grid->column('status', '状态')->display(function($status) use ($arr) {
            //0-待提交，10-第一次驳回待重新提交，20-第一次提交待审核，25-第一次通过，30-复审驳回，35-复审待审核，40-第二次通过,50-已结算\r\n，60-已取消

            return $arr[$status];
        });

        $grid->column('real_return_money', '实返');
        $grid->actions(function ($actions) {

            // 去掉删除
            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();

            if( $actions->row->status == 32 ){
                $actions->add(new RefuseAuditOk());
                $actions->add(new RefuseAuditFail());
            }
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $order = UsersOrders::findOrFail($id);
        $order->imgs = $order->imgs?explode(',', $order->imgs):[];
        $show = new Show($order);

        $show->field('store_title', __('店铺名称'));
        $show->field('order_no', __('订单号'));
        $show->store_img()->image(config('app.url'));
        $show->field('amount', '买家实付');
        $show->field('user.name', '买家名称');
        $show->field('user.phone', '买家手机号');
        $show->field('created_at', '下单时间');
        $show->field('max_money', '满');
        $show->field('return_money', '返');
        $show->divider();
        $show->field('real_return_money', '返现金额');
        $show->field('fee', '服务费');
        $show->field('discount_fee', '优惠')->as(function($fee){
            return '服务费-'.$fee;
        });
        $show->field('settle_amount', '订单金额');

        $show->field('user_order_no', '初审：用户订单号');
        if( count($order->imgs) ){
            $show->field('imgs', '初审截图')->carousel(300, 400, config('app.url'));
        }
        
        $show->weight('复审：分量够吗')->using(['不够', '够', '无评价']);
        $show->taste('味道如何')->using(['一般', '好', '无评价']);
        $show->leak('是否撒漏')->using(['有', '无撒漏', '无评价']);
        $show->field('advise', '意见反馈');
        if( $order->img2 ){
            $show->img2('评价截图')->image(config('app.url'), 400);
        }
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UsersOrders());

        $form->number('settlement_id', __('Settlement id'));
        $form->number('uid', __('Uid'));
        $form->number('store_id', __('Store id'));
        $form->number('store_info_id', __('Store info id'));
        $form->number('sub_id', __('Sub id'));
        $form->text('order_no', __('Order no'));
        $form->text('store_title', __('Store title'));
        $form->text('store_img', __('Store img'));
        $form->decimal('amount', __('Amount'))->default(0.00);
        $form->text('user_order_no', __('User order no'));
        $form->text('imgs', __('Imgs'));
        $form->decimal('max_money', __('Max money'))->default(0.00);
        $form->decimal('return_money', __('Return money'))->default(0.00);
        $form->decimal('real_return_money', __('Real return money'))->default(0.00);
        $form->switch('status', __('Status'));
        $form->text('fail_reason', __('Fail reason'));
        $form->switch('taste', __('Taste'));
        $form->switch('weight', __('Weight'));
        $form->switch('leak', __('Leak'));
        $form->text('advise', __('Advise'));
        $form->text('img2', __('Img2'));
        $form->switch('is_need_ali_audit', __('Is need ali audit'));
        $form->datetime('sub_time1', __('Sub time1'))->default(date('Y-m-d H:i:s'));
        $form->datetime('sub_time2', __('Sub time2'))->default(date('Y-m-d H:i:s'));
        $form->datetime('overdue', __('Overdue'))->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
